package exo1;

import exo1.exeption.UnfounedElementException;


public class GenericList <T> implements Checked{


	private Maillon<T> head=null;
	public void remove(T value) throws UnfounedElementException {
		  Maillon<T>tmp=head;
		  Maillon<T>pred=head;
		    while(tmp!=null){
		    	if(tmp.value.equals(value)){
		    		break;
		    	}
		    	pred=tmp;
		    	tmp=tmp.next;
		    }
		    if(tmp==null)
		    	throw new UnfounedElementException("l'elemnt à supprimer n'existe pas");
		    if(tmp==head){
		    	head=head.next;
		    }else
		    pred.next=tmp.next;
		
	}
	public void add(T value) {
		Maillon<T> elem= new Maillon<T>(value);
		elem.next=head;
		head=elem;
		
	}
	@Override
	public void invariant() {
		//assurer ques les T redifine la fonction equal
	}
	@Override
	public String toString() {
		String s="[";
	    String sep="";
	    Maillon<T>tmp=head;
	    while(tmp!=null){
	    	s+=sep+tmp.value;
	    	tmp=tmp.next;
	    	sep=",";
	    }
		s+="]";
		return s;
	}
	public class Maillon<T>{
		private T value;
		private Maillon next=null;
		public Maillon(T value2) {
			value=value2;
		}
		@Override
		public String toString() {
			return value.toString();
		}
	}
	public static void main(String[] args) {
		
		try { // vérification du test d'assertion
			assert false;
			throw new RuntimeException("JVM option '-ea'");
			}catch (AssertionError e){}
		
		GenericList<String>list=new GenericList<String>();
		assert list.toString().equals("[]"):list;
		list.add("zero");
		assert list.toString().equals("[zero]"):list.toString();
		list.add("abdel");
		assert list.toString().equals("[abdel,zero]"):list;
		try {
			list.remove("zerox");
		} catch (UnfounedElementException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		assert list.toString().equals("[abdel]"):list;
		System.out.println("toute les tests bien réussi");
		
	}

}
